package com.q.springboot.framework.config.redisson;

import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RTopic;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Map;

/**
 * @author Ming
 * @date 2020/9/15 11:22
 */
public abstract class AbstractQueue {

    Map<String, RDelayedQueue<? super Serializable>> offQueue = RMessageListener.offQueue;

    Map<String, RBlockingDeque<? super Serializable>> messageQueue = RMessageListener.messageQueue;

    Map<String, RTopic> topicMap = RMessageListener.topicMap;

    protected RDelayedQueue<? super Serializable> getRDelayedQueue() {
        return offQueue.get(shortName());
    }

    protected RBlockingDeque<? super Serializable> getMessageQueue() {
        return messageQueue.get(shortName());
    }

    private String shortName() {
        Annotation[] annotations = this.getClass().getAnnotations();
        RMessage rMessage = Arrays.stream(annotations).filter(annotation -> annotation instanceof RMessage)
                .map(annotation -> (RMessage) annotation).findAny().get();
        String queryName = rMessage.queue();
        String topicName = rMessage.topic();
        return topicName + "." + queryName;
    }

    protected RTopic getTopic() {
        return topicMap.get(shortName());
    }

}
